<?php

include("ctrials.class.php");
include("ctrialsdata.class.php");

class BAL
{
    public $service;
    public $params;

    private $totalRecords;

    function  __construct()
    {
        $this->service = new CTrials();
    }

    public function getSearchResults($queryString)
    {
        $keyword = $this->getSearchKeyword($queryString);

        $count = $this->getSearchCount();

        $start = $this->getStartRecord();

        $results_array = array();

        $query = "\"" . str_replace("*", "", $keyword) . "\"";

        $records = $this->service->ClinicalSearch(urlencode($query), $count, $start);

        if($records["count"])
        {
            $this->totalRecords = $records["count"];
        }

        foreach($records->clinical_study as $clinical_study)
        {
            $record = new ClinicalData();

            $record->id = $clinical_study->nct_id;
            $record->title = $clinical_study->title;
            $record->status = $clinical_study->status;
            $record->lastChanged = $clinical_study->last_changed;
            $record->url = $clinical_study->url;

            $results_array[] = $record;
        }

        return $results_array;
    }

    public function getTotalRecords()
    {
        return $this->totalRecords;
    }

    public function getSearchKeyword($queryString)
    {
        $searchKeyword = "";

        $this->params = $this->getQueryStringValues($queryString);

        if($this->params)
        {
            if(isset ($this->params["searchValue"]))
            {
                if(is_array($this->params["searchValue"]) && count($this->params["searchValue"]) > 0)
                {
                    $searchKeyword = $this->params["searchValue"][0];
                }
                else
                {
                    $searchKeyword = $this->params["searchValue"];
                }
            }
        }

        return $searchKeyword;
    }

    public function getSearchCount()
    {
        $searchCount = 10;

        if($this->params)
        {
            if(isset ($this->params["numberOfRows"]))
            {
                if(is_array($this->params["numberOfRows"]) && count($this->params["numberOfRows"]) > 0)
                {
                    $searchCount = $this->params["numberOfRows"][0];
                }
                else
                {
                    $searchCount = $this->params["numberOfRows"];
                }
            }
        }

        return $searchCount;
    }

    public function getStartRecord()
    {
        $startRecord = 1;

        if($this->params)
        {
            if(isset ($this->params["startRow"]))
            {
                if(is_array($this->params["startRow"]) && count($this->params["startRow"]) > 0)
                {
                    $startRecord = $this->params["startRow"][0];
                }
                else
                {
                    $startRecord = $this->params["startRow"];
                }
            }
        }

        return $startRecord;
    }

    public function getQueryStringValues($query_string)
    {
        $query  = explode('&', $query_string);
        $params = array();

        foreach( $query as $param )
        {
            list($name, $value) = explode('=', $param);
            $params[urldecode($name)][] = urldecode($value);
        }

        return $params;
    }
}
?>
